//
//  BGOwnAuctionController.m
//  bigMall
//
//  Created by LTOVE on 16/9/18.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGOwnAuctionController.h"
#import "BGOwnAuctionCell.h"

#import "BGOwnAuctionModel.h"
#import "BGDateResult.h"
#import "BGAuctionInfoViewController.h"

#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"

@interface BGOwnAuctionController (){
    
    NSMutableArray *dataArray;
    NSInteger _thisPage;
    NSInteger _allPage;
}

@end

@implementation BGOwnAuctionController

- (instancetype)init
{
    if (self = [super init]) {
        _thisPage = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.header beginRefreshing];
    
}

- (void)loadData
{
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult ownAuctionWithThisPage:@"1" pageNum:@"10" success:^(NSObject *result) {
        [MBProgressHUD hideHUD];
        [self.tableView.header endRefreshing];
        BGOwnAuctionModel *model = (BGOwnAuctionModel *)result;
        dataArray = [NSMutableArray arrayWithArray:model.list];
        [self.tableView reloadData];
        if ([model.allPage integerValue] > _thisPage) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
            _thisPage = 2;
            _allPage = [model.allPage integerValue];
        }
    } failure:^(NSError *errot) {
        [MBProgressHUD hideHUD];
        
    }];
}

- (void)loadMoreData
{
    if (_thisPage > _allPage) {
        [self.tableView.footer endRefreshingWithNoMoreData];
        return;
    }
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult ownAuctionWithThisPage:[NSString stringWithFormat:@"%ld",(long)_thisPage] pageNum:@"10" success:^(NSObject *result) {
        [MBProgressHUD hideHUD];
        [self.tableView.footer endRefreshing];
        BGOwnAuctionModel *model = (BGOwnAuctionModel *)result;
        [dataArray addObjectsFromArray:model.list];
        [self.tableView reloadData];
        if (model.list.count) {
            _thisPage++;
        }
        
    } failure:^(NSError *errot) {
        
        [MBProgressHUD hideHUD];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BGOwnAuctionCell *cell = [BGOwnAuctionCell cellWithTableView:tableView];
    BGOwnAuctionModelRes *model = dataArray[indexPath.row];
    cell.model = model;
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BGOwnAuctionModelRes *model = dataArray[indexPath.row];
    return model.cellHeigh;
}
#pragma mark - ⬇️ cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BGOwnAuctionModelRes *model = dataArray[indexPath.row];
    BGAuctionInfoViewController *vc = [BGAuctionInfoViewController new];
    GWLog(@"%@",model.idStr);
    vc.goodId = model.goods_id;
    vc.title = @"拍卖详情";
    UIBarButtonItem *bac = [UIBarButtonItem new];
    bac.title = @"";
    self.navigationItem.backBarButtonItem = bac;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
